package com.kstudio.ksnapshot.kedit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

import com.kstudio.ksnapshot.KUtil;
import com.kstudio.ksnapshot.KSnapShotAsync;
import com.kstudio.ksnapshot.KSnapShotAsync.KSnapShotAsyncListener;
import com.kstudio.ksnapshot.KSnapshot;
import com.kstudio.ksnapshot.R;
import com.kstudio.ksnapshot.kedit.KEditControl.KEditControlListener;
import com.kstudio.ksnapshot.kedit.KEditVideoPlayer.KEditVideoPlayerListener;

public class KEditActivity extends Activity implements KEditControlListener, KSnapShotAsyncListener, KEditVideoPlayerListener {
    KUtil kLog = new KUtil("KEditActivity");

    KSnapshot kSnapshot;

    String kFilePath;
    String kOutPath;
    KEditVideoPlayer kEditVideoPlayer;
    KEditControl kEditControl;
    ImageView kEditAnimation;
    AnimationSet kFlash;

    private KSnapShotAsync kSnapShotAsync;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        kLog.i("onCreate");

        setContentView(R.layout.kedit_activity);

        Intent intent = getIntent();
        if(intent != null) {
            kFilePath = intent.getStringExtra("path");
        }
        kSnapshot = (KSnapshot)getApplicationContext();
        kOutPath = kSnapshot.getStoragePath();

        kSnapShotAsync = null;
        kFlash = null;

        kEditVideoPlayer = (KEditVideoPlayer) findViewById(R.id.keditvideoplayer);
        kEditVideoPlayer.setVideoPlayerListener(this);
        kEditVideoPlayer.setVideoPath(kFilePath);

        kEditControl = (KEditControl) findViewById(R.id.keditcontrol);
        kEditControl.KSetOnEditControlListener(this);

        kEditAnimation = (ImageView) findViewById(R.id.keditanimation);
        kEditAnimation.setBackgroundColor(0xFFFFFFFF);
        kEditAnimation.setVisibility(View.GONE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_klist, menu);
        return true;
    }

    @Override
    public void onSingleSaveClick() {
        if(kSnapShotAsync == null) {
//            kSnapShotAsync = new KSnapShotAsync();
//            kSnapShotAsync.KSnapShotAsyncListener(this);
//            kSnapShotAsync.execute(kSnapShotAsync.KCreateKSnapShotAsyncParm(KSnapShotAsync.KEDIT_GET_SINGLE_SNAPSHOT, kFilePath, kOutPath, kEditVideoPlayer.KgetCurrentTime()));
//            CreateShinyAnimation();
        }
    }

    @Override
    public void onSeekViewChangeTime(int t) {
        // TODO Auto-generated method stub
        kLog.e("onSeekViewChangeTime = %d",t);
        kEditVideoPlayer.KseekTo(t);
    }

    @Override
    public void OnProgressUpdate() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onKEditVideoPlayerActive() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onKEditVideoPlayerError() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onKEditVideoPlayerDuration(int duration) {
        kEditControl.KSetDuration(duration);
    }

    private void CreateShinyAnimation() {
        if(kFlash == null) {
            kFlash = new AnimationSet(true);

            Animation a1 = new AlphaAnimation(0.0f, 1.0f);
            a1.setDuration(1000);
            kFlash.addAnimation(a1);

            Animation a2 = new AlphaAnimation(1.0f, 0.0f);
            a2.setDuration(1000);
            kFlash.addAnimation(a2);

            kFlash.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    kLog.i("onAnimationStart");
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    kLog.i("onAnimationEnd");
                    kEditAnimation.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    kLog.i("onAnimationRepeat");
                }
            });
        }
        kEditAnimation.setVisibility(View.VISIBLE);
        kEditAnimation.startAnimation(kFlash);
    }
}
